﻿using System;
using System.Linq;
using SapaFinance.Domain.Model;
using Xunit;

namespace SapaFinance.Tests
{
    public class CorporationTest
    {
        public static Corporation Create()
        {
            var corporation = new Corporation(UserTest.Create());
            corporation.Tin="ИНН";
            return corporation;
        }

        [Fact]
        public void Fill()
        {
            var agent = AgentTest.Create();
            var corporation = Create();
            corporation.Name="Имя клиента";
            corporation.Description="Описание юр.лица";
            corporation.FullName="Полное имя клиента";
            corporation.SetAgent(agent);
            corporation.RegistrationDate=DateTime.Now;
            corporation.LegalAddress="Юридический адрес";
            corporation.ActualAddress="Фактический адрес";
            corporation.Okonh="ОКОНХ";
            corporation.Okpo="ОКПО";
            corporation.DirectorName="ФИО директора";
            corporation.AccountantName="ФИО главного бухгалтера";
            Assert.Equal("Имя клиента", corporation.Name);
            Assert.Equal("Описание юр.лица", corporation.Description);
            Assert.Equal("Полное имя клиента", corporation.FullName);
            Assert.True(agent.Equals(corporation.Agent));
            Assert.NotNull(corporation.RegistrationDate);
            Assert.Equal("Юридический адрес", corporation.LegalAddress);
            Assert.Equal("Фактический адрес", corporation.ActualAddress);
            Assert.Equal("ОКОНХ", corporation.Okonh);
            Assert.Equal("ОКПО", corporation.Okpo);
            Assert.Equal("ФИО директора", corporation.DirectorName);
            Assert.Equal("ФИО главного бухгалтера", corporation.AccountantName);
            Assert.Equal("ИНН", corporation.Tin);
        }

        [Fact]
        public void CheckCorporationCreate()
        {
            var corporation = Create();
            Assert.NotNull(corporation);
            Assert.Equal(0, corporation.Accounts.Count);
            Assert.True(UserTest.Create().Equals(corporation.CreatedBy.User));
            Assert.Equal(0, corporation.Accounts.Count);
        }

        [Fact]
        public void AddPrimaryAccountToCorporation()
        {
            //arrange
            var corporation = Create();
            var bank = BankTest.Create();
            var user = UserTest.Create();
            //act
            var rubAccount = new Account("345345", new Rub(), bank,corporation,user);
            var eurAccount = new Account("345345345345", new Eur(), bank, corporation, user);
            var usdAccount = new Account("65765", new Usd(), bank, corporation, user);
            corporation.AddAccount(rubAccount);
            corporation.AddAccount(eurAccount);
            eurAccount.SetPrimary(true);
            corporation.ChangeAccount(eurAccount);
            corporation.AddAccount(usdAccount);
            //assert
            Assert.True(eurAccount.IsPrimary);
            Assert.False(rubAccount.IsPrimary);
            Assert.False(usdAccount.IsPrimary);
            Assert.True(corporation.Accounts.Where(x=>x.IsPrimary).Contains(eurAccount));
            Assert.True(corporation.Accounts.Where(x => !x.IsPrimary).Contains(rubAccount));
            Assert.True(corporation.Accounts.Where(x => !x.IsPrimary).Contains(usdAccount));
        }
        [Fact]
        public void ChangePrimaryAccountInCorporation()
        {
            //arrange
            var corporation = Create();
            var bank = BankTest.Create();
            var user = UserTest.Create();
            var rubAccount = new Account("345345", new Rub(), bank, corporation, user);
            var eurAccount = new Account("345345345345", new Eur(), bank, corporation, user);
            var usdAccount = new Account("65765", new Usd(), bank, corporation, user);
            corporation.AddAccount(rubAccount);
            eurAccount.SetPrimary(true);
            corporation.ChangeAccount(eurAccount);
            corporation.AddAccount(eurAccount);
            corporation.AddAccount(usdAccount);
            //act
            usdAccount.SetPrimary(true);
            corporation.ChangeAccount(usdAccount);
            //assert
            Assert.True(corporation.Accounts.Where(x => x.IsPrimary).Contains(usdAccount));
            Assert.True(corporation.Accounts.Where(x => !x.IsPrimary).Contains(rubAccount));
            Assert.True(corporation.Accounts.Where(x => !x.IsPrimary).Contains(eurAccount));
        }

        [Fact]
        public void CheckCorporationEqual()
        {
            var corporation = Create();
            var corporation1 = Create();
            Assert.True(corporation1.Equals(corporation));
        }

        //[Fact]
        //public void CheckCorporationDelete()
        //{
        //    var user = UserTest.Create();
        //    var corporation = Create();
        //    corporation.Delete(user);
        //    Assert.True(user.Equals(corporation.DeletedBy.User));
        //}
    }
}